package pt.iscte.pcd.buttonadder;

import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JFrame;

public class ButtonAdder {

	private JFrame window;
	private JButton add_button;
	private AddButtonListener listener;

	public ButtonAdder() {
		window = new JFrame("Button adder");
		add_button = new JButton("Add new button");
		listener = new AddButtonListener(this);
		Container container = window.getContentPane();
		container.setLayout(new FlowLayout());
		container.add(add_button);
		add_button.addActionListener(listener);
		window.setSize(200, 200);
		window.setLocation(200, 100);
		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	public void createNewButton(int current_button_number) {
		// Create a new button:
		JButton new_button = new JButton("Button " + current_button_number);
		// Change the appearance of the button:
		new_button.setForeground(Color.GREEN);
		new_button.setBackground(Color.BLACK);
		// Add this listener to the button so that the button sends an
		// event
		// whenever it is pressed:
		new_button.addActionListener(new RemoveButtonListener(this));
		window.getContentPane().add(new_button);
		// validate() causes the content pane to re-layout the buttons:
		window.getContentPane().validate();

	}

	public void removeButton(JButton button_pressed) {
		// The source of the event is the button that was pressed
		// and that we should remove:

		// We remove the button from the content pane:
		window.getContentPane().remove(button_pressed);
		// Ask the content pane to eventually redraw itself:
		window.getContentPane().repaint();
		// validate() causes the content pane to re-layout the buttons:
		window.getContentPane().validate();

	}

	public Object getAddButton() {
		return add_button;
	}
	
	public void execute() {
		window.setVisible(true);
	}

	public static void main(String[] argumentos) {
		ButtonAdder button_adder = new ButtonAdder();
		button_adder.execute();
	}

	

}
